package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.dao.RoleDao;
import com.gxa.dao.impl.RoleDaoImpl;
import com.gxa.pojo.Department;
import com.gxa.pojo.Role;
import com.gxa.pojo.User;
import com.gxa.service.RoleService;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao = new RoleDaoImpl();

    @Override
    public JsonResult queryRoleList(Integer currentPage, Integer pagesize) {
        JsonResult jsonResult = new JsonResult();

        try {
            Integer totalsize = roleDao.countRole();
            Integer p1 = (currentPage-1)*pagesize;
            Integer p2 = pagesize;
            List<Role> result = roleDao.queryRoleList(p1, p2);
            for (Role role : result) {
                int department_id = role.getDepartment_id();
                Department department = roleDao.getDepartment(department_id);
                role.setDepartment(department);
            }

            if (result!=null){
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(totalsize);
                jsonResult.setData(result);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员！");
        }
        return jsonResult;
    }

    @Override
    public JsonResult queryRoleList(Integer currentPage, Integer pagesize, String role_name) {
        JsonResult jsonResult = new JsonResult();

        try {
            Integer totalsize = roleDao.countRole(role_name);
            Integer param01 = (currentPage-1)*pagesize;
            Integer param02 = pagesize;
            List<Role> list = roleDao.queryRoleList(param01, param02, role_name);
            for (Role role : list) {
                int department_id = role.getDepartment_id();
                Department department = roleDao.getDepartment(department_id);
                role.setDepartment(department);
            }
            if(list!=null){
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功！");
                jsonResult.setCount(totalsize);
                jsonResult.setData(list);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员！");
        }
        return jsonResult;
    }

    @Override
    public R delRoleById(String[] ids) {
        R r = new R();
        try {
            if (ids.length>0){
                for (String id : ids) {
                    roleDao.delRoleById(id);
                }
            }
            r.setCode(200);
            r.setMsg("删除成功！");

        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }

    @Override
    public R addRole(Role role) {
        R r = new R();
        try {
            Integer result = roleDao.addRole(role);
            if(result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Override
    public R updateRole(Role role) {
        R r = new R();
        try {
            Integer result = roleDao.updateRole(role);
            if(result>0){
                r.setCode(200);
                r.setMsg("修改成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }


}
